'use server'

import * as z from 'zod'
import bcrypt from 'bcryptjs'

import prisma from '@/lib/prisma'
import { getUserByEmail } from '@/data/user'
import { RegisterSchema } from '@/schemas/auth'
import { sendVerificationEmail } from '@/lib/mail'
import { generateVerificationToken } from '@/lib/tokens'

export const register = async (values: z.infer<typeof RegisterSchema>) => {
    const validatedFields = RegisterSchema.safeParse(values)

    if (!validatedFields.success) {
        return { error: 'Invalid fields!' }
    }

    const { email, password, name } = validatedFields.data
    const hashedPassword = await bcrypt.hash(password, 10)

    const existingUser = await getUserByEmail(email)
    if (existingUser) {
        return { error: 'Email already in use!' }
    }

    await prisma.user.create({
        data: { name, email, password: hashedPassword }
    })

    const verificationToken = await generateVerificationToken(email)
    const result = await sendVerificationEmail(
        verificationToken.email,
        verificationToken.token
    )

    return { success: 'Confirmation email sent!' }
}